package com.zjw.contorller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import com.zjw.domain.vo.ResponseResult;
import com.zjw.service.UserService;
import com.zjw.system.SystemConstant;
import com.zjw.system.SystemReturnEnum;
import com.zjw.utils.JWTUtil;
import com.zjw.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

@RestController
public class UploadController {


    @Autowired
    private UserService userService;

    /**
     * 上传头像
     * 当前用户名作为文件名, 上传成功后返回头像网络地址，在数据库中更新头像数据
     *
     * @param file    文件
     * @param request 要求
     * @return {@link ResponseResult}
     */
    @PostMapping("/uploadAvatar")
    public ResponseResult uploadAvatar(MultipartFile file, HttpServletRequest request) {
//        TODO 文件类型判断
        if (file == null || FileUtil.isDirectory(file.getOriginalFilename()))
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);

        String temp = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();

        Long jwtId = JWTUtil.getJWTId(request.getHeader("token"));
        String fileName = String.valueOf(jwtId);
//        TODO 判断文件拓展名类型
        if (!temp.equals(".jpg") && !temp.equals(".jpeg") && !temp.equals(".png"))
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);

        fileName += temp;

        try {
            String resPath = OSSUtil.uploadFile(file.getInputStream(), SystemConstant.OSS_AVATAR_PATH, fileName);
            System.out.println("id:" + jwtId.toString() + "====fileName:" + fileName + "====resPath:" + resPath);
            userService.updateAvatar(jwtId, resPath);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(SystemReturnEnum.FILE_OPS_ERR);
        }

    }

    @PostMapping("/uploadImage")
    public ResponseResult uploadImage(MultipartFile file, HttpServletRequest request) {
        // 判断文件类型是否合法
        if (file == null || FileUtil.isDirectory(file.getOriginalFilename()))
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);

        // 获取拓展名
        String temp = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();
        if (!temp.equals(".jpg") && !temp.equals(".jpeg") && !temp.equals(".png"))
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);


        System.out.println("上传图片：" + file.getOriginalFilename());
        try {
            String resPath = OSSUtil.uploadFile(file.getInputStream(),
                    SystemConstant.OSS_MESSAGE_GROUP_IMG_PATH,
                    System.currentTimeMillis() + temp);
            JSONObject entries = new JSONObject();
            entries.putOpt("resPath", resPath);
            return ResponseResult.ok(entries);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseResult.error(SystemReturnEnum.FILE_OPS_ERR);
    }
}
